package com.example.qjia.ui.activity;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TimePicker;

import com.example.qjia.Presenters.AddHolidayPresenter;
import com.example.qjia.Presenters.AddHolidayPresenterImp;
import com.example.qjia.R;
import com.example.qjia.adapter.TeachersAdapter;
import com.example.qjia.basic.BaseActivity;
import com.example.qjia.data.romotedata.Teacher;
import com.example.qjia.ui.ViewInterface.AddHolidayViewInterface;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class AddHolidayActivity extends BaseActivity implements View.OnClickListener,AddHolidayViewInterface,TeachersAdapter.TeacherChooseListener {
    private LinearLayout backly;
    private LinearLayout commitly;
    private EditText reason;
    private TextView beginDate;
    private TextView endDate;
    private TextView beginTime;
    private TextView endTime;
    private TextView chooseTeacher;
    private int year ;
    private int monthOfYear;
    private int dayOfMonth;
    private int mhour;
    private int minute;
    private AddHolidayPresenter addHolidayPresenter;
    private List<Teacher> teachers;
    private TeachersAdapter adapter;
    private Dialog dialog;
    private int tid=0;
    private int byear;
    private int bmonth;
    private int bday;
    private int bhour;
    private int bminute;// 标记开始时间，用来判断时间的合理性

    private int eyear;
    private int emonth;
    private int eday;
    private int ehour;
    private int eminute;// 标记结束时间，用来判断时间的合理性
    private static final String TAG="AddHolidayActivity";



    @Override
    protected int getLayoutResourceId() {
        return R.layout.activity_add_holiday;
    }

    @Override
    protected void init() {
        backly=(LinearLayout)findViewById(R.id.back_ly);
        commitly=(LinearLayout)findViewById(R.id.commit_ly);
        reason=(EditText)findViewById((R.id.request_reason));
        beginDate=(TextView)findViewById(R.id.b_date);
        beginTime=(TextView)findViewById(R.id.b_time);
        endDate=(TextView)findViewById(R.id.e_date);
        endTime=(TextView)findViewById(R.id.e_time);
        chooseTeacher=(TextView)findViewById(R.id.teacher);
        chooseTeacher.setOnClickListener(this);
        backly.setOnClickListener(this);
        commitly.setOnClickListener(this);
        beginDate.setOnClickListener(this);
        beginTime.setOnClickListener(this);
        endDate.setOnClickListener(this);
        endTime.setOnClickListener(this);
        Calendar calendar=Calendar.getInstance();
        year=calendar.get(Calendar.YEAR);
        monthOfYear=calendar.get(Calendar.MONTH)+1;
        dayOfMonth=calendar.get(Calendar.DAY_OF_MONTH);
        mhour=calendar.get(Calendar.HOUR_OF_DAY);
        minute=calendar.get(Calendar.MINUTE);
        String smonth=monthOfYear+"";
        String sday=dayOfMonth+"";
        String shour=mhour+"";
        String sminute=minute+"";
        if(monthOfYear<10)smonth="0"+smonth;
        if(dayOfMonth<10)sday="0"+sday;
        if(mhour<10)shour="0"+shour;
        if(minute<10)sminute="0"+sminute;
        beginDate.setText(year+"-"+smonth+"-"+sday);
        endDate.setText(year+"-"+smonth+"-"+sday);
        beginTime.setText(shour+":"+sminute);
        endTime.setText(shour+":"+sminute);           //设置时间的初始值
        byear=year;                  //标记开始时间的初始值
        bmonth=monthOfYear;
        bday=dayOfMonth;
        bhour=mhour;
        bminute=minute;

        eyear=year;                  //标记结束时间的初始值
        emonth=monthOfYear;
        eday=dayOfMonth;
        ehour=mhour;
        eminute=minute;
        addHolidayPresenter=new AddHolidayPresenterImp(this);

    }

    @Override
    protected void initView() {

    }

    @Override
    public void onClick(View view) {
        if (view.getId()==R.id.b_date){
            addHolidayPresenter.showBeginDayDialog();


        }
        if(view.getId()==R.id.e_date)addHolidayPresenter.showEndDayDialog();
        if(view.getId()==R.id.b_time)addHolidayPresenter.showBeginTime();
        if(view.getId()==R.id.e_time)addHolidayPresenter.showEndTime();
        if(view.getId()==R.id.teacher){
            System.out.println("teacher");
            addHolidayPresenter.showChooseTeacher();
        }
        if(view.getId()==R.id.back_ly)finish();
        if(view.getId()==R.id.commit_ly){
            Log.i(TAG, "onClick: "+byear+bmonth+bday+bhour+bminute+"----"+eyear+emonth+eday+ehour+eminute+"tid="+tid);
            String beginDT=beginDate.getText().toString()+" "+beginTime.getText().toString();
            String endDT=endDate.getText().toString()+" "+endTime.getText().toString();
            String reasons=reason.getText().toString();

            if(byear<eyear||(byear==eyear&&bmonth<emonth)||(bday<eday&&byear==eyear&&bmonth==emonth)||(byear==eyear&&bmonth==emonth&&bday==eday&&bhour<ehour)||(byear==eyear&&bmonth==emonth&&bday==eday&&bhour==ehour&&bminute<eminute)){  //时间的合理性，结束时间必须大于开始时间
                if(tid!=0){
                    if(!reasons.equals("")){
                        System.out.println("可以提交");     //符合提交条件
                        addHolidayPresenter.commitHoliday(beginDT,endDT,reasons,tid);
                    }else {
                        toast("理由不能为空");
                    }

                }else {
                    toast("请选择申请老师");
                }

            }else {
                toast("开始时间不能大于开始时间");
            }



        }
    }

    @Override
    public void showProgress() {

    }

    @Override
    public void hideProgress() {

    }

    @Override
    public void backToMain() {
        toast("提交成功");
        this.finish();

    }

    @Override
    public void showBeginTimeDialog() {
         TimePickerDialog timePickerDialog=new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker timePicker, int hour, int fen) {
                bhour=hour;
                bminute=fen;
                String hours=""+hour;
                String minutes=""+fen;
                if(hour<10)
                   hours="0"+hours;
                if(fen<10)
                    minutes="0"+fen;
                beginTime.setText(hours+":"+minutes);

            }
        },mhour,minute,true);
        timePickerDialog.show();

    }

    @Override
    public void showEndTimeDialog() {

        TimePickerDialog timePickerDialog=new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker timePicker, int hour, int fen) {
                ehour=hour;
                eminute=fen;
                String hours=""+hour;
                String minutes=""+fen;
                if(hour<10)
                    hours="0"+hours;
                if(fen<10)
                    minutes="0"+fen;
                endTime.setText(hours+":"+minutes);

    }
},mhour,minute,true);
        timePickerDialog.show();
    }

    @Override
    public void showChooseTeacherDialog() {


        teachers=new ArrayList<>();

        adapter=new TeachersAdapter(teachers,this);
        adapter.setTeacherChooseListener(this);
        dialog=new Dialog(this);
        View view= LayoutInflater.from(this).inflate(R.layout.add_holiday_teacher_dialog,null);
        RecyclerView teacherView=(RecyclerView)view.findViewById(R.id.chooseTecherView);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
        teacherView.setLayoutManager(layoutManager);
        addHolidayPresenter.loadTeachers();
        teacherView.setAdapter(adapter);
        Window window=dialog.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();

        lp.width = 300; // 宽度
        lp.height = 300; // 高度
        lp.alpha = 0.7f; // 透明度
        window.setAttributes(lp);
        window.setContentView(view);
        dialog.setContentView(view);
        dialog.show();



    }

    @Override
    public void setTeacherDialogData(List<Teacher> teacheres){   //设置老师窗口的数据源

        System.out.println("activity:"+teacheres.get(0).getName());
        if(teachers==null){
            teachers=new ArrayList<>();
        }
        teachers.addAll(teacheres);     //添加获取到的数据到Adapter的数据中
        adapter.notifyDataSetChanged();//通知数据源的数据已经发生了改变
        adapter.notifyItemRemoved(adapter.getItemCount());


    }




    @Override
    public void showdaychooseError() {

    }

    @Override
    public void showBeginDayDialog() {
        DatePickerDialog dialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year1, int month, int day) {
                System.out.println(year+""+monthOfYear+""+dayOfMonth);
                byear=year1;
                bmonth=month;
                bday=day;
                String smonth=month+"";
                String sday=day+"";
                if(month<10)smonth="0"+smonth;
                if(day<10)sday="0"+sday;


                System.out.println(year1+""+month+""+day);


                beginDate.setText(year1+"-"+smonth+"-"+sday);





            }
        },year,monthOfYear, dayOfMonth);
        dialog.show();
    }

    @Override
    public void showEndDayDialog() {
        DatePickerDialog dialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year1, int month, int day) {
                System.out.println(year+""+monthOfYear+""+dayOfMonth);
                eyear=year1;
                emonth=month;
                eday=day;

                System.out.println(year1+""+month+""+day);
                String smonth=month+"";
                String sday=day+"";
                if(month<10)smonth="0"+smonth;
                if(day<10)sday="0"+sday;
                System.out.println(year1+""+month+""+day);
                endDate.setText(year1+"-"+smonth+"-"+sday);
            }
        },year,monthOfYear, dayOfMonth);
        dialog.show();

    }

    @Override
    public void showCommitError() {
        toast("提交失败");

    }

    @Override
    public void setTeacher(String teacherName, int Tid) {
        this.tid=Tid;
        System.out.println("activity:"+teacherName);
        chooseTeacher.setText(teacherName);
        dialog.dismiss();

    }
}
